﻿Imports System.Data.OleDb
Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM Profiles WHERE Name=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                
                If Not IsDBNull(dr("Email")) Then tb_email.Text = dr("Email")
                If Not IsDBNull(dr("Description")) Then tb_Description.Text = dr("Description")
                
            End If
        End If
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim newFileName As String = ""
        Dim SqlString As String
        If upload_pic.HasFile Then
            Dim myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() & ".jpg"
            Dim fileLocationOnServerHardDisk = Request.MapPath("pictures") & "/" & newFileName
            upload_pic.SaveAs(fileLocationOnServerHardDisk)
        End If
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        If upload_pic.HasFile Then
            SqlString = "UPDATE Profiles SET Email=@f1,Description=@f2,Picture=@f3 WHERE Name=@f4"
        Else
            SqlString = "UPDATE Profiles SET Email=@f1,Description=@f2 WHERE Name=@f4"
        End If


        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        

        cmd.Parameters.AddWithValue("@f1", tb_email.Text)
        cmd.Parameters.AddWithValue("@f2", tb_Description.Text)
        If upload_pic.HasFile Then cmd.Parameters.AddWithValue("@f3", "pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f4", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("Profile.aspx?Name=" & User.Identity.Name)
    End Sub
End Class